<template>
  <div class="editForm-wrapper">
    <!-- 承运商编辑dialog-->
    <el-dialog :title="title" width="570px" :close-on-click-modal="false" v-dialogDrag :visible.sync="editVisible">
      <el-form label-width="108px" :rules="dataRule" ref="editForm" :model="editForm">
        <el-form-item label="承运商名称:" prop="companyName" :rules="[
                  {required: true, message:'请选择承运商', trigger:'change'}
                 ]">
          <el-input class="select" v-model="editForm.companyName" @focus="getCompanyClient" placeholder="请选择承运商"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="footer-input">
          <el-button size="small" type="primary" @click="doSubmit()" v-noMoreClick>确定</el-button>
          <el-button size="small" @click="close()">取消</el-button>

        </div>

      </span>
    </el-dialog>
    <CompanyDialog ref="CompanyDialog" @getCompanyData="getCompanyData"></CompanyDialog>
  </div>
</template>
<script>
  import {
    edit,
    checkRepeat
  } from '@/api/market/crm.js'
  import CompanyDialog from '@/components/dialog/truckcompanyDialog.vue'
  export default {
    components: {
      CompanyDialog
    },
    data () {
      return {
        method: '',
        title: '',
        editForm: {
          companyName: '',
          companyId: '',
          company: {
            id: ''
          },
          truck: {
            id: ''
          }
        },
        editVisible: false,
        dialogVisible: false,
        dataRule: {
          companyName: [{
            required: true,
            message: '请输入承运商名称',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
    	handleClose(done) {
			this.$Msg.confirm({
				title:'您确定要关闭吗？',
				content:this.GLOBAL.FORM_CLOSE_TIPS,
				confirmCall:()=>{
					done();
				}
			})
		},
      init (method, truckId) {
        if (method === 'add') {
          this.editVisible = true
          this.method = method
          this.$nextTick(() => {
            this.$refs.editForm.resetFields()
            this.$nextTick(() => {})
          })

          this.editForm.truck.id = truckId
          this.title = '设置承运商'
        } else if (method === 'edit') {
          this.title = '修改承运商'
        }
      },
      close () {
        this.editVisible = false
      },
      // 承运商选择弹窗
      getCompanyClient () {
        this.$refs['CompanyDialog'].dialogVisible = true
      },
      // 客户选择数据回显
      getCompanyData (row) {
        this.editForm.companyName = row.names
        this.editForm.companyId = row.id
        this.editForm.company.id = row.id
        this.dialogVisible = false
      },
      doSubmit () {
        this.$refs['editForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: `/truck/truck/saveCompany`,
              method: 'post',
              data: this.editForm
            }).then((res) => {
              if (res.data.code === '200') {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                })
                this.editVisible = false
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    },
    mounted () {

    }
  }
</script>
<style lang="scss" scoped>
.editForm-wrapper{
    ::v-deep.el-dialog{
        .el-input.select .el-input__inner{
            cursor: pointer;
        }
    }
}
</style>
